package com.shuo.dsa.niuke;

import com.shuo.dsa.model.TreeNode;

/**
 * 输入一棵二叉树，判断该二叉树是否是平衡二叉树
 */
public class IsTreeBalanced {
    public boolean IsBalanced_Solution(TreeNode root) {
        if (root == null) return true;
        return (Math.abs(getDepth(root.left) - getDepth(root.right)) <= 1);
    }

    private int getDepth(TreeNode root) {
        if (root == null) return 0;
        return Math.max(getDepth(root.left), getDepth(root.right)) + 1;
    }
}
